(function() {
    var $ = require(L('$')), _ = require(L('_'));

    var win = $.createWindow(L('win_ugd_details'));
    win.navbar = $.ui('navbar')(true, true);
    var ui = $.layout('ugd/details/wrapper');
    ui.top.add($.ui('partial').scan_and_take_photo());
    win.add([win.navbar, ui.view]);

    win.display = function(ugd) {
        $.removeChildren(ui.content);
        ui.content.add(Layout(ugd));
    };

    function Layout(ugd) {
        var ui = $.layout('ugd/details/content');
        ui.title.text = ugd.RetailerName;
        ui.price.text = ugd.FormattedPrice ? ugd.FormattedPrice : '-';
        $.cache_image(ugd.ImagePath, null, function(e) {
            var blobImage = Ti.UI.createImageView({
                image : e
            }).toImage();
            ui.image.image = e;
            var height = blobImage.height * 112 / blobImage.width;
            ui.image.updateLayout({
                height : height
            });
            ui.imageBorder.updateLayout({
                height : height + 10
            });
            ui.titleComment.updateLayout({
                height : 40,
                width : 280
            });
        });
        $.on('load', ui.image, function() {
            ui.images.remove(ui.loading);
        });
        $.click(ui.titleComment, function() {
            if (ui.titleComment.expanded == true) {
                ui.titleComment.backgroundImage = L('bg_arrow_right');
                ui.labelComment.height = 0;
                $.updateHeight([ui.view]);
                ui.titleComment.expanded = false;
            } else {
                ui.titleComment.backgroundImage = L('bg_arrow_down');
                ui.labelComment.height = Ti.UI.SIZE;
                $.updateHeight([ui.view]);
                ui.titleComment.expanded = true;
            }
        });
        ui.labelComment.text = ugd.Comment;
        $.cache_image(ugd.RetailerLogo, ui.retailerLogo);
        $.tap(ui.share, function() {
            $.ui('popup').share({
                fb : {
                    link : ugd.ShareFbLink,
                    name : ugd.ShareFbName,
                    message : '',
                    caption : ugd.ShareFbCaption,
                    description : ugd.ShareFbDescription,
                    picture : ugd.ShareFbPicture,
                },
                twitter : {
                    message : ugd.ShareTwitterMessage
                },
                pinterest : {
                    link : ugd.ShareFbLink,
                    picture : ugd.ShareFbPicture,
                    description : ugd.SharePinterestDescription
                },
                email : {
                    desireID : ugd.DesireID
                }
            }, function(e) {
                $.needToReloadMyDesires(true);
                if (e == $.enum('share').Pinterest)
                    ui.iconPinterest.image = L('icon_pinterest_on');
                if (e == $.enum('share').Facebook)
                    ui.iconFacebook.image = L('icon_facebook_on');
                if (e == $.enum('share').Twitter)
                    ui.iconTwitter.image = L('icon_twitter_on');
                if (e == $.enum('share').Email) {
                    ui.iconEmail.image = L('icon_email_on');
                    return;
                }
                $.webservice().updateShareUGD(ugd.DesireID, e, function(response) {
                    var result = JSON.parse(response);
                    if (result.Success)
                        $.a(L('ugd_share_success'), L('ugd_success_title'));
                });
            }).open();
        });
        if (ugd.PinterestShareTimes > 0)
            ui.iconPinterest.image = L('icon_pinterest_on');
        if (ugd.FacebookShareTimes > 0)
            ui.iconFacebook.image = L('icon_facebook_on');
        if (ugd.TwitterShareTimes > 0)
            ui.iconTwitter.image = L('icon_twitter_on');
        if (ugd.EmailShareTimes > 0)
            ui.iconEmail.image = L('icon_email_on');
        return ui.view;
    };
    module.exports = win;
})();
